﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using POSSystem.DAL;
using System.Data.SqlClient;
using System.Collections.Specialized;
using System.Data;

namespace POSSystem.BLL
{
    public class RepairModelProblems : _RepairModelProblems
    {
        public static void InitForNewModel(int modelID)
        {
            try
            {
                Problems p = Problems.GetAllActiveProblems();
                RepairModelProblems rp = new RepairModelProblems();
                for (int i = 0; i < p.RowCount; i++)
                {
                    rp.AddNew();
                    rp.RPP = 0;
                    rp.StandardPrice = 0;
                    rp.SpecialPrice = 0;
                    rp.ProblemID = p.ID;
                    rp.RepairModelID = modelID;
                    rp.BranchID = Branches.CurrentBranchID;
                    p.MoveNext();
                }

                rp.Save();
                
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("RepairModelProblems.cs", "InitForNewModel", ex.StackTrace);
            }
        }

        public static RepairModelProblems GetProblemsByModelID(int modelID)
        {
            try
            {
                RepairModelProblems p = new RepairModelProblems();
                ListDictionary parameters = new ListDictionary();

                parameters.Add(new SqlParameter("@modelID", SqlDbType.Int, 0), modelID);

                p.LoadFromSql("GetRepairModelProblems", parameters);

                return p;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("RepairModelProblems.cs", "GetProblemsByModelID", ex.StackTrace);
                throw ex;
            }
        }

        public static RepairModelProblems GetProblem(int modelID, int problemID)
        {
            try
            {
                RepairModelProblems rp = new RepairModelProblems();
                rp.Where.ProblemID.Value = problemID;
                rp.Where.RepairModelID.Value = modelID;
                rp.Where.BranchID.Value = Branches.CurrentBranchID;
                rp.Query.Load();

                return rp;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("RepairModelProblems.cs", "GetProblem", ex.StackTrace);
                throw ex;
            }
        }

        public static RepairModelProblems GetAllActiveProblem(int branchID)
        {
            try
            {
                RepairModelProblems rp = new RepairModelProblems();
                rp.Where.BranchID.Value = branchID;
                rp.Query.Load();

                return rp;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("RepairModelProblems.cs", "GetAllActiveProblem", ex.StackTrace);
                throw ex;
            }
        }
    }
}
